# BackendConnection - Connection to the Entertainer backend
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

from backend.core.message import Message
from backend.core.message_bus_proxy import MessageBusProxy
from backend.core.message_type_priority import MessageType, MessagePriority
from backend.core.message_handler import MessageHandler

class BackendConnection(MessageHandler):
    """
    BackendConnection - Connection to the Entertainer backend. Instance from
    this class is a gate to backend messagebus.
    """

    def __init__(self):
        """Initialize connection. Connects to backend."""
        #messages = {MessageType.CONTENT_CONF_UPDATED : MessagePriority.NORMAL,
        #            MessageType.PREFERENCES_CONF_UPDATED : MessagePriority.NORMAL,
        #            MessageType.FEED_DB_UPDATED : MessagePriority.HIGH,
        #            MessageType.GUIDE_DB_UPDATED : MessagePriority.HIGH,
        #            MessageType.RECORDING_DB_UPDATED : MessagePriority.HIGH }
        messages = {} #FIXME: Above is the real thing
        name = "Entertainer Frontend"
        self.message_bus_proxy = MessageBusProxy(messages, self, name)
        self.message_bus_proxy.connectToMessageBus()
        self.message_bus_proxy.start()
        self.message_bus_proxy.sendMessage(Message(MessageType.FRONTEND_OPENED))
    
    def close_connection(self):
        """Close connection to backend"""
        self.message_bus_proxy.sendMessage(Message(MessageType.FRONTEND_CLOSED))
        self.message_bus_proxy.disconnectFromMessageBus()
    
    def request_feed_update(self):
        """Request backend to fetch all feeds from the Internet."""
        self.message_bus_proxy.sendMessage(Message(MessageType.UPDATE_FEEDS))
        
    def request_guide_Uupdate(self):
        """Request backend to update TV-Guide from the Internet."""
        self.message_bus_proxy.sendMessage(Message(MessageType.UPDATE_GUIDE))
        
    def request_weather_update(self):
        """Request backend to update weather information from the Internet."""
        self.message_bus_proxy.sendMessage(Message(MessageType.UPDATE_WEATHER))
    
    # Implements MessageHandler interface
    def handleMessage(self, message):
        """Handle received messages. (Implements MessageHandler interface)"""
        if message.get_type() == MessageType.CONTENT_CONF_UPDATED:
            pass
        elif message.get_type() == MessageType.PREFERENCES_CONF_UPDATED:
            pass
        elif message.get_type() == MessageType.FEED_DB_UPDATED:
            pass
        elif message.get_type() == MessageType.GUIDE_DB_UPDATED:
            pass
        elif message.get_type() == MessageType.RECORDING_DB_UPDATED:
            pass
                    
    def getName(self):
        return "Entertainer's Backend connection"   
